home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / geometer / filestuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.9 KB  |  382 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <strings.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <pwd.h>
  26. #include <fcntl.h>
  27. #include "generic.h"
  28. #include <gl.h>
  29.  
  30. static char newfilename[300];
  31. static char expandedname[300];
  32. char currentpath[300];
  33.  
  34. char currentfilename[300];
  35.  
  36. /* flags: */
  37.  
  38. long    VIEW_ONLY = 0, view_only = 0;
  39. long    MAKE_BACKUPS = 0;
  40. long    MAKE_CHECKPOINTS = 0;
  41. long    foreground_flag = 0, dirtyfile_flag = 0;
  42.  
  43. long    xorg, yorg, xsize, ysize;
  44.  
  45. /*
  46.  * Expand ~ to home dorectory
  47.  */
  48.  
  49. char *tilde(char *expandedname, char *file)
  50. {
  51.     char *c, *p;
  52.     struct passwd *pw;
  53.     char login[40];
  54.  
  55.     if (file[0] != '~')
  56.         return (strcpy(expandedname, file));
  57.  
  58.     for (p = login, c = &file[1]; *c && *c != '/'; *p++ = *c++)
  59.         ;
  60.     *p = '\0';
  61.     if (login[0] == '\0')
  62.         (void) strcpy(expandedname, getenv("HOME"));
  63.     else {
  64.         pw = getpwnam(login);
  65.         if (pw == 0)
  66.             return (0);
  67.         (void) strcpy(expandedname, pw->pw_dir);
  68.     }
  69.     (void) strcat(expandedname, c);
  70.     return expandedname;
  71. }
  72.  
  73. long openstatus(char *filename)
  74. {
  75.     struct stat buf;
  76.     long    uid, gid;
  77.     int            fi;
  78.  
  79.     if (tilde(expandedname, filename) == 0) return 0;
  80.     
  81.     uid = geteuid();
  82.     gid = getegid();
  83.     if (-1 == stat(expandedname, &buf)) {
  84.     if (-1 == (fi = creat(expandedname, 0664)))
  85.         return 0;
  86.     close(fi);
  87.     unlink(expandedname);
  88.     return CAN_WRITE_FILE;
  89.     }
  90.     if ((buf.st_mode & S_IFREG) == 0)
  91.     return FILE_EXISTS;
  92.     if ((buf.st_uid == uid && (buf.st_mode & S_IWRITE)) ||
  93.     (buf.st_gid == gid && (buf.st_mode & S_IWGRP)) ||
  94.     (buf.st_mode & S_IWOTH))
  95.         return FILE_EXISTS | CAN_WRITE_FILE;
  96.     else
  97.     return FILE_EXISTS;
  98. }
  99.  
  100. void makewintitle()
  101. {
  102.     char title[220], *tail;
  103.  
  104.     if (*currentfilename)
  105.     sprintf(title, "%s (ver %s): %s", PROG_NAME, PROG_VERSION, currentfilename);
  106.     else
  107.     sprintf(title, "%s (ver %s): (no file name)", PROG_NAME, PROG_VERSION);
  108.     if (dirtyfile_flag) strcat(title, "*");
  109.     if (view_only)
  110.     strcat(title, "[View Only]");
  111.     wintitle(title);
  112.     tail = strrchr(currentfilename, '/');
  113.     tail = (tail == NULL) ? currentfilename: tail+1;
  114.     icontitle(tail);
  115. }
  116.  
  117. /* setdirtyfile(), cleardirtyfile() mark the file dirty and clean
  118.  *  and resets the window title, if necessary.
  119.  */
  120.  
  121. void setdirtyfile()
  122. {
  123.     if (dirtyfile_flag == 0) {
  124.     dirtyfile_flag = 1;
  125.     makewintitle();
  126.     }
  127. }
  128.  
  129. void cleardirtyfile()
  130. {
  131.     if (dirtyfile_flag == 1) {
  132.     dirtyfile_flag = 0;
  133.     makewintitle();
  134.     }
  135. }
  136.  
  137. /* checkfile() returns 0 if the filename is not to be put in the
  138.  * browsegizmo.  This default version shows all file names.
  139.  */
  140.  
  141. long checkfile(char *filename)
  142. {
  143.     return (*filename != 0);
  144. }
  145.  
  146. long collectfilename(char *label, char *newname)
  147. {
  148.     getfilename(label, newname, 0, currentpath, "Open", checkfile);
  149.     if (*newname) return 1;
  150.     return 0;
  151. }
  152.  
  153. long dirtyfilecheck()
  154. {
  155.     if (dirtyfile_flag) {
  156.     switch(message("File modified; save the changes?",
  157.                                     "Yes", "No", "Cancel")) {
  158.         case 1:
  159.             handlesavecmd();
  160.         if (dirtyfile_flag) return 0;
  161.         return 1;
  162.         case 2:
  163.         return 1;
  164.         case 3:
  165.             return 0;
  166.     }
  167.     }
  168.     return 1;
  169. }
  170.  
  171. void handlenewcmd()
  172. {
  173.     if (VIEW_ONLY) {
  174.     message("New not allowed in view only mode", 0, 0, 0);
  175.     return;
  176.     }
  177.     if (dirtyfilecheck() == 0) return;
  178.     if (entries.clearbuffer) entries.clearbuffer();
  179.     dirtyfile_flag = 0;
  180.     view_only = 0;
  181.     currentfilename[0] = 0;
  182.     makewintitle();
  183.     return;
  184. }
  185.  
  186. void doopen(char *filename)
  187. {
  188.     switch (openstatus(filename)) {
  189.     case 0:
  190.         if (VIEW_ONLY) {
  191.             message("No such file", 0, 0, 0);
  192.             return;
  193.         }
  194.         message("Non-existant, unwriteable file\n", 0, 0, 0);
  195.         return;
  196.     case FILE_EXISTS:
  197.         if (entries.clearbuffer) entries.clearbuffer();
  198.         view_only = 1;
  199.         strcpy(currentfilename, expandedname);
  200.         if (entries.readfile) entries.readfile(currentfilename);
  201.         dirtyfile_flag = 0;
  202.         break;
  203.     case CAN_WRITE_FILE:
  204.         if (VIEW_ONLY) {
  205.             message("No such file", 0, 0, 0);
  206.             return;
  207.         }
  208.         message("New file", 0, 0, 0);
  209.         if (VIEW_ONLY == 0) view_only = 0;
  210.         if (entries.clearbuffer) entries.clearbuffer();
  211.         strcpy(currentfilename, expandedname);
  212.         dirtyfile_flag = 0;
  213.         break;
  214.     case FILE_EXISTS | CAN_WRITE_FILE:
  215.         if (VIEW_ONLY == 0) view_only = 0;
  216.         if (entries.clearbuffer) entries.clearbuffer();
  217.         strcpy(currentfilename, expandedname);
  218.         if (entries.readfile) entries.readfile(currentfilename);
  219.         writebackupfile();
  220.         dirtyfile_flag = 0;
  221.         break;
  222.     }
  223. }
  224.  
  225. void handleopencmd()
  226. {
  227.     if (dirtyfilecheck() == 0) return;
  228.     if (collectfilename("Open File:", newfilename) == 0) return;
  229.     doopen(newfilename);
  230.     makewintitle();
  231. }
  232.  
  233. void handlesavecmd()
  234. {
  235.     if (view_only) {
  236.         message("View only mode", 0, 0, 0);
  237.     return;
  238.     }
  239.     if (currentfilename[0] == 0) {
  240.     if (collectfilename("Save As:", newfilename) == 0) return;
  241.     if (openstatus(newfilename) & CAN_WRITE_FILE) {
  242.         strcpy(currentfilename, expandedname);
  243.         } else {
  244.         message("Can't write file", 0, 0, 0);
  245.         return;
  246.     }
  247.     } else if ((openstatus(currentfilename) & CAN_WRITE_FILE) == 0) {
  248.     view_only = 1;
  249.     message("Can't write file", 0, 0, 0);
  250.     return;
  251.     }
  252.     if (entries.writefile)
  253.         if (entries.writefile(currentfilename) == 0) return;
  254.     dirtyfile_flag = 0;
  255.     makewintitle();
  256. }
  257.  
  258. void handlesaveascmd()
  259. {
  260.     if (collectfilename("Save As:", newfilename) == 0) return;
  261.     if (openstatus(newfilename) & CAN_WRITE_FILE) {
  262.     strcpy(currentfilename, expandedname);
  263.     } else {
  264.     message("Can't write file", 0, 0, 0);
  265.     return;
  266.     }
  267.     if (entries.writefile)
  268.         if (entries.writefile(currentfilename) == 0) return;
  269.     dirtyfile_flag = 0;
  270.     if (VIEW_ONLY == 0) view_only = 0;
  271.     makewintitle();
  272. }
  273.  
  274. void handleinsertcmd()
  275. {
  276.     if (view_only) {
  277.         message("View only mode", 0, 0, 0);
  278.     return;
  279.     }
  280.     if (collectfilename("Insert File:", newfilename) == 0) return;
  281.     if (openstatus(newfilename) & FILE_EXISTS) {
  282.     if (entries.insertfile) entries.insertfile(newfilename);
  283.     } else {
  284.     message("Can't read file", 0, 0, 0);
  285.     }
  286.     makewintitle();
  287. }
  288.  
  289. void handlequitcmd()
  290. {
  291.     if (dirtyfilecheck() == 0) return;
  292.     if (entries.quit) entries.quit();
  293.     exit(0);
  294. }
  295.  
  296. void handlehelpcmd()
  297. {
  298.     if (entries.help)
  299.         entries.help();
  300.     else
  301.         message("No help available", 0, 0, 0);
  302. }
  303.  
  304. static inerror = 0;
  305.  
  306. void handleerrorwrite(long sigtype)
  307. {
  308.     char filename[100], msg[200];
  309.     char *tmpdir;
  310.  
  311.     if (inerror) return;    /* Hopeless -- error occurred during write */
  312.     inerror = 1;
  313.     tmpdir = (char *)getenv("TMPDIR");
  314.     if (tmpdir == 0) tmpdir = "/tmp";
  315.     sprintf(filename, "%s/%s.crash", tmpdir, PROG_NAME);
  316.     sprintf(msg, "Yer gonna crash (%d): Try to save as %s?",
  317.                                                     sigtype, filename);
  318.     if (message(msg, "Yes", "No", 0) == 1)
  319.         if (entries.errorwritefile) entries.errorwritefile(filename);
  320. }
  321.  
  322. /* print stuff: generate a print name from the pid and
  323.  * an increasing number.  Put the result in $TMPDIR.
  324.  */
  325.  
  326. static long printindex = 1;
  327.  
  328. void handleprintcmd()
  329. {
  330.     char printfilename[100];
  331.     char *tmpdir;
  332.  
  333.     long pid = (long)getpid();
  334.     tmpdir = (char *)getenv("TMPDIR");
  335.     if (tmpdir == 0) tmpdir = "/tmp";
  336.     sprintf(printfilename, "%s/%s%4.4x%4.4x", tmpdir, PROG_NAME, pid, printindex++);
  337.     if (entries.printfile)
  338.         if (0 == entries.printfile(printfilename)) {
  339.         message("Print failed", "Confirm", 0, 0);
  340.     }
  341. }
  342.  
  343. static void defaultmakecheckpointname(char *s)
  344. {
  345.     sprintf(s, "%s.ckp", currentfilename);
  346. }
  347.  
  348. static void defaultmakebackupname(char *s)
  349. {
  350.     sprintf(s, "%s.bak", currentfilename);
  351. }
  352.  
  353. void writecheckpointfile()
  354. {
  355.     char filename[300];
  356.  
  357.     if (MAKE_CHECKPOINTS) {
  358.         if (currentfilename[0] == 0) return;
  359.     if (entries.makecheckpointname)
  360.         entries.makecheckpointname(filename);
  361.     else
  362.         defaultmakecheckpointname(filename);
  363.     if ((openstatus(filename) & CAN_WRITE_FILE) && entries.writefile)
  364.         entries.writefile(filename);
  365.     }
  366. }
  367.  
  368. void writebackupfile()
  369. {
  370.     char filename[300];
  371.  
  372.     if (MAKE_BACKUPS) {
  373.         if (currentfilename[0] == 0) return;
  374.     if (entries.makebackupname)
  375.         entries.makebackupname(filename);
  376.     else
  377.         defaultmakebackupname(filename);
  378.     if ((openstatus(filename) & CAN_WRITE_FILE) && entries.writefile)
  379.         entries.writefile(filename);
  380.     }
  381. }
  382.